package data;

import java.text.DecimalFormat;
import java.util.List;

import bo.BusinessFigure;
import domain.Equation;

/**
 * this class will calculate from math coordinate system to pixel
 * this class will calculate the full equation for regression
 * @author Simon Korteweg
 * @author Nikolai Straessle
 * @author Marco Serpi
 *
 */
public class PaintGraph{
	
	private List<BusinessFigure> aList;
	private double gx0 = 10;
	private double gy0;
	private double height;
	private double width;
	private double xFaktor;
	private int xs;
	private double visibleYPoints;
	private Equation eq;
	private DecimalFormat f = new DecimalFormat("#0.00"); 
	
	/**
	 * Constructor
	 * @param List<BusinessFigure> list
	 * 		java.util.List<BusinessFigure>
	 * @param double height
	 * 		Double
	 * @param double width
	 * 		Double
	 * @param double scale
	 * 		Double
	 */
	public PaintGraph(List<BusinessFigure> list, double height, double width, double scale) {
		this.aList = list;
		this.height = height;
		this.width = width;
		this.gy0 = ((height - (2*gx0)) / 2);
		this.visibleYPoints = scale;
		eq = new Equation(aList);
	}
	
	/**
	 * Use this for Revenue Actions
	 * print the graphical y-Value for the input x-coordinate
	 * Use this for graph
	 * @param x-coordinate
	 * @return
	 * 		integer
	 */
	public int getRevenueY(int x){
		int i = 0;
		double mathData[] = eq.getRevenueEquation();
		double val1 = (mathData[0] * x);
		double val2 = (mathData[0] * mathData[1]);
		double val3 = mathData[2];
		i = (int)(val1 - val2 + val3);
		return getViewY(i);
	}
	
	/**
	 * Use this for Revenue Actions
	 * print the real y-Value for the input x-coordinate
	 * @param x-coordinate
	 * @return
	 * 		formatted double
	 */
	public double getRevenueTab(int x){
		double i = 0;
		double mathData[] = eq.getRevenueEquation();
		double val1 = (mathData[0] * x);
		double val2 = (mathData[0] * mathData[1]);
		double val3 = mathData[2];
		i = (val1 - val2 + val3);
		return Double.parseDouble(f.format(i));
	}
	/**
	 * Use this for profit Actions
	 * print the graphical y-Value for the input x-coordinate
	 * Use this for graph
	 * @param x-coordinate
	 * @return
	 * 		integer
	 */
	public int getProfitY(int x){
		int i = 0;
		double mathData[] = eq.getProfitEquation();
		double val1 = (mathData[0] * x);
		double val2 = (mathData[0] * mathData[1]);
		double val3 = mathData[2];
		i = (int)(val1 - val2 + val3);
		return getViewY(i);
	}
	
	/**
	 * Use this for profit Actions
	 * print the real y-Value for the input x-coordinate
	 * @param x-coordinate
	 * @return
	 * 		formatted double
	 */
	public double getProfitTab(int x){
		double i = 0;
		double mathData[] = eq.getProfitEquation();
		double val1 = (mathData[0] * x);
		double val2 = (mathData[0] * mathData[1]);
		double val3 = mathData[2];
		i = (val1 - val2 + val3);
		return Double.parseDouble(f.format(i));
	}

	/**
	 * print the graphical value for the x=0 coordinate
	 * @return
	 * 		integer
	 */
	public int getGx0() {
		return (int)gx0;
	}

	/**
	 * print the graphical value for the y=0 coordinate
	 * @return
	 * 		integer
	 */
	public int getGy0() {
		return (int)gy0;
	}
	
	/**
	 * print a graphical x-coordinate
	 * @param x-coordinate
	 * @return
	 * 		integer
	 */
	public int getViewX(double xx)
	{
		int fensterBreite = (int)width;
		xFaktor = (fensterBreite / (6));
		xs = (int)Math.round((xx - 0) * xFaktor);
		return xs;
	}

	/**
	 * print a graphical y-coordinate
	 * @param y-coordinate
	 * @return
	 * 		integer
	 */
	public int getViewY(double yy)
	{
		int fensterHoehe = (int)height;
		int ys = (int)(Math.round((visibleYPoints + (-1)*yy) * (fensterHoehe / (2 * visibleYPoints))));
		return ys;
	}
}
